package controller;

import common.*;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import ui.Main;
import util.FlushCourse;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.URL;
import java.util.Calendar;
import java.util.ResourceBundle;

public class DeleStuController implements Initializable {
    private Socket socket= Main.userClientService.getSocket();
    @FXML
    private Label name;
    @FXML
    private Label birth;
    @FXML
    private Label number;
    @FXML
    private Label phone;
    @FXML
    private Label email;
    private User student;
    private Course course;
    private TeacherCourseController teacherCourseController;
    public DeleStuController(User student,Course course,TeacherCourseController teacherCourseController) {
        this.student = student;
        this.course=course;
        this.teacherCourseController=teacherCourseController;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        name.setText(student.getName());
        birth.setText(student.getBirth());
        number.setText(student.getNumber());
        phone.setText(student.getPhone());
        email.setText(student.getId());
    }
    @FXML
    public void dele(){
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle("警告");
        alert.setHeaderText("删除学生");
        alert.setContentText("确定要进行该操作吗？");
        ButtonType buttonTypeOK = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        alert.getButtonTypes().setAll(buttonTypeOK);
        alert.setOnCloseRequest(event -> {
            if (alert.getResult() == buttonTypeOK) {
                Message ms=new Message();
                ms.setState(MessageType.Stu_Dele);
                try {
                    ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(ms);
                    oos.writeObject(new StuMidClass(student.getId(),course));
                    System.out.println("请求删除学生");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Cancel();
            }
        });
        alert.showAndWait();
    }
    @FXML
    public void Cancel(){
        Stage stage=(Stage) name.getScene().getWindow();
        stage.close();
    }
}
